<?php

namespace PhpYes\Modules\Log\Example;

use PhpYes\Modules\Log\LogLevel;
use PhpYes\Modules\Log\LogRecordBase;

class AppLogRecord extends LogRecordBase
{
    private static $skipFilePartials = array(
        '/wgx/php_yes/Modules/Log/',
    );

    //private static $skipFunctions = array(
    //    'call_user_func',
    //    'call_user_func_array',
    //);

    private static $host;
    private static $reqMethod;
    private static $reqHost;
    private static $reqPath;
    private static $reqQuery;
    private static $reqBody;

    private $file;
    private $line;
    private $class;
    private $function;

    /**
     * @param string $level - refer LogLevel
     * @param string $message
     * @return void
     */
    public function log($level, $message)
    {
        $this->level = $level;
        $this->message = $message;
        if (empty(self::$host)) {
            self::$host = gethostname();
        }
        if (empty(self::$reqMethod)) {
            self::$reqMethod = \PhpYes\Yes::$p->getRequest()->getMethod();
        }
        if (empty(self::$reqHost)) {
            self::$reqHost = Pp::getRequest()->getHost();
        }
        if (empty(self::$reqPath)) {
            self::$reqPath = \PhpYes\Yes::$p->getRequest()->getPath();
        }

        if (empty(self::$reqQuery)) {
            $query = \PhpYes\Yes::$p->getRequest()->getQueryParams();
            if (empty($query)) {
                self::$reqQuery = 'empty';
            } else {
                self::$reqQuery = json_encode($query, JSON_UNESCAPED_UNICODE);
            }
        }
        if (empty(self::$reqBody)) {
            $body = \PhpYes\Yes::$p->getRequest()->getBodyParams();
            if (empty($body)) {
                self::$reqBody = 'empty';
            } else {
                if (is_array($body)) {
                    self::$reqBody = json_encode($body, JSON_UNESCAPED_UNICODE);
                } else {
                    self::$reqBody = strval($body);
                }
            }
        }

        $traceArr = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

        // skip first since it's always the current method
        array_shift($traceArr);

        $i = 0;
        while (isset($traceArr[$i])) {
            $trace = $traceArr[$i];
            // $type = isset($trace['type']) ? $trace['type'] : null;
            $file = empty($trace['file']) ? null : trim($trace['file']);
            // $line = $trace['line'];
            // $class = isset($trace['class']) ? $trace['class'] : null;
            // $function = isset($trace['function']) ? $trace['function'] : null;

            if ($file) {
                $isSkiped = false;
                foreach (self::$skipFilePartials as $partial) {
                    if (strpos($file, $partial) !== false) {
                        $isSkiped = true;
                        break;
                    }
                }
                if ($isSkiped) {
                    $i++;
                    continue;
                }
            }

            //if ($function) {
            //    if (in_array($function, self::$skipFunctions)) {
            //        $i++;
            //        continue;
            //    }
            //}

            break;
        }

        $this->file = isset($traceArr[$i]['file']) ? $traceArr[$i]['file'] : null;
        $this->line = isset($traceArr[$i]['line']) ? $traceArr[$i]['line'] : null;
        $this->class = isset($traceArr[$i + 1]['class']) ? $traceArr[$i + 1]['class'] : null;
        $this->function = isset($traceArr[$i + 1]['function']) ? $traceArr[$i + 1]['function'] : null;
    }

    /**
     * get 最终日志内容
     * @return string
     */
    public function getLogMessage()
    {
        $dt = $this->dt ? $this->dt : new \DateTime();
        $level = $this->level ? $this->level : LogLevel::ERROR;
        $tag = $this->getTag();
        if (empty($tag)) {
            $tag = '';
        }
        $msg = $this->message ? $this->message : 'empty';

        return json_encode(array(
            'time' => $dt->format('Y-m-d H:i:s'),
            'level' => $level,
            'tag' => $tag,
            'host' => self::$host ? self::$host : '',
            'reqMethod' => self::$reqMethod ? self::$reqMethod : '',
            'reqHost' => self::$reqHost ? self::$reqHost : '',
            'reqPath' => self::$reqPath ? self::$reqPath : '',
            'reqQuery' => self::$reqQuery ? self::$reqQuery : '',
            'reqBody' => self::$reqBody ? self::$reqBody : '',
            'file' => $this->file ? $this->file : '',
            'line' => $this->line ? $this->line : 0,
            'class' => $this->class ? $this->class : '',
            'function' => $this->function ? $this->function : '',
            'msg' => str_replace(array("\r\n", "\r", "\n"), ' ', $msg)
        ), JSON_UNESCAPED_UNICODE);
    }
}
